#include "colorset_mt.h"

#include <bool.h>
#include <bstring.h>
#include <colorset.h>
#include <dynamictypemanager.h>
#include <enumeration.h>
#include <number.h>
#include <typefactory.h>
#include <wfnet.h>

#include <QtTest/QTest>

using namespace core;
using namespace wfnet;
using namespace moduletest;

void ColorSet_mt::initTestCase()
{
    EnumerationValues vals;
    vals << "black" << "red" << "blue";
    Enumeration colors("EColors", vals);
    BasicString objects("tail");

    TypeFactory::getInstance()->registerType(colors);

    m_net = new WfNet(this);

    DynamicTypeManager::getInstance()->registerColorSet("Colors",
        colors, m_net);
    DynamicTypeManager::getInstance()->registerColorSet("Objects",
        objects, m_net);
}

void ColorSet_mt::matches()
{
    Bool b1(true), b2(false);
    Number n1(0), n2(1);
    BasicString s1("myFirstString"), s2("mySecondString");

    ColorSet c1 = *DynamicTypeManager::getInstance()->getColorSet("Colors",
        m_net);
    ColorSet c2 = *DynamicTypeManager::getInstance()->getColorSet("Colors",
        m_net);

    c1.setValue("black");
    c2.setValue("red");

    // valid
    QVERIFY(b1.matches(b2));
    QVERIFY(n1.matches(n2));
    QVERIFY(s1.matches(s2));
    QVERIFY(c1.matches(c2));
    QVERIFY(c1.matches(Enumeration::NAME));
    QVERIFY(s1.matches(BasicString::NAME));

    // not valid
    QVERIFY(!n1.matches(b1));
    QVERIFY(!n1.matches(s1));
    QVERIFY(!n1.matches(c1));
    QVERIFY(!b1.matches(s1));
    QVERIFY(!b1.matches(c1));
    QVERIFY(!c1.matches(s1));
}

void ColorSet_mt::operatorEquals()
{
    Bool b1(true), b2(false);
    Number n1(0), n2(1);
    BasicString s1("myFirstString"), s2("mySecondString");

    ColorSet c1 = *DynamicTypeManager::getInstance()->getColorSet("Colors",
        m_net);
    ColorSet c2 = *DynamicTypeManager::getInstance()->getColorSet("Objects",
        m_net);
    ColorSet c3 = *DynamicTypeManager::getInstance()->getColorSet("Colors",
        m_net);

    c1.setValue("black"),
    c2.setValue("pony"),
    c3.setValue("red");

    // not equal
    QVERIFY(!(b1 == b2));
    QVERIFY(!(n1 == n2));
    QVERIFY(!(s1 == s2));
    QVERIFY(!(c1 == c2));
    QVERIFY(!(c2 == c3));
    QVERIFY(!(c1 == c3));

    b2.setValue(true);
    n2.setValue(0);
    s2.setValue("myFirstString");
    c3.setValue("black");

    // equal
    QVERIFY(b1 == b2);
    QVERIFY(n1 == n2);
    QVERIFY(s1 == s2);
    QVERIFY(c1 == c3);
}

void ColorSet_mt::operatorNotEquals()
{
    Bool b1(true), b2(false);
    Number n1(0), n2(1);
    BasicString s1("myFirstString"), s2("mySecondString");

    ColorSet c1 = *DynamicTypeManager::getInstance()->getColorSet("Colors",
        m_net);
    ColorSet c2 = *DynamicTypeManager::getInstance()->getColorSet("Objects",
        m_net);
    ColorSet c3 = *DynamicTypeManager::getInstance()->getColorSet("Colors",
        m_net);

    c1.setValue("black"),
    c2.setValue("pony"),
    c3.setValue("red");

    // not equal
    QVERIFY(b1 != b2);
    QVERIFY(n1 != n2);
    QVERIFY(s1 != s2);
    QVERIFY(c1 != c2);
    QVERIFY(c2 != c3);
    QVERIFY(c1 != c3);

    b2.setValue(true);
    n2.setValue(0);
    s2.setValue("myFirstString");
    c3.setValue("black");

    // equal
    QVERIFY(!(b1 != b2));
    QVERIFY(!(n1 != n2));
    QVERIFY(!(s1 != s2));
    QVERIFY(!(c1 != c3));
}

void ColorSet_mt::cleanupTestCase()
{
    DynamicTypeManager::getInstance()->unregisterColorSet("Colors", m_net);
    DynamicTypeManager::getInstance()->unregisterColorSet("Objects", m_net);
}
